package com.frcdi.qms.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.frcdi.qms.common.config.QmsConfig;
import com.frcdi.qms.common.config.DruidDataConfig;
import com.frcdi.qms.common.config.jdbc.JdbcUtils;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.common.utils.sql.MysqlBackupUtils;
import com.frcdi.qms.system.domain.DatabaseBackup;
import com.frcdi.qms.system.service.IDatabaseBackupService;
import com.frcdi.qms.system.service.ISysUserOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author cell
 */
@Component("Task")
public class Task
{
    @Autowired
    private DruidDataConfig druidDataSource;

    @Autowired
    private ISysUserOperLogService operLogService;

    @Autowired
    private IDatabaseBackupService databaseBackupService;

    @Autowired
    JdbcUtils jdbcUtils;





    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void haha()
    {
        System.out.println("哈哈哈");
    }

    //全量备份数据库
    public void backupFullDatabase() {
        Map<String, String> dbInfo = jdbcUtils.getDatabaseBackupDBInfo();
        DatabaseBackup databaseBackup = new DatabaseBackup();
        //先进行数据库备份
        String savePath = QmsConfig.getBackupPath();
        String fileName = DateUtil.format(new Date(), "yyyyMMdd-HHmmss") + "-" + RandomUtil.randomNumbers(4) + ".sql";
        boolean flag = MysqlBackupUtils.backup(dbInfo.get("host"), druidDataSource.getUsername(), druidDataSource.getPassword(), savePath, fileName, dbInfo.get("dbName"),dbInfo.get("port"));
        if (flag) {
            databaseBackup.setName(fileName);
            databaseBackup.setRemark(savePath);
            Long fileSize = FileUtil.file(new File(savePath, fileName)).length();
            String dbSize = "";
            DecimalFormat df = new DecimalFormat("#");
            if (fileSize < 1024) {
                dbSize = df.format((double) fileSize) + "BT";
            } else if (fileSize < 1048576) {
                dbSize = df.format((double) fileSize / 1024) + "KB";
            } else if (fileSize < 1073741824) {
                dbSize = df.format((double) fileSize / 1048576) + "MB";
            } else {
                dbSize = df.format((double) fileSize / 1073741824) + "GB";
            }
            databaseBackup.setDbSize(dbSize);
            databaseBackup.setRemark(savePath);
            databaseBackup.setCreateBy(null!= SecurityUtils.getAuthentication() ? SecurityUtils.getUsername() : "定时任务-全量备份");
            databaseBackupService.insertDatabaseBackup(databaseBackup);
        }
    }

    //增量备份数据库
    public void backupIncrementDatabase() {
        MysqlBackupUtils.incrementalBackup();
    }

    /**
     * 获取主机地址
     */
    private String getHost() {
        String url = druidDataSource.getUrl();
        if(url.indexOf("3306")>=0){
            return url.substring(url.indexOf("mysql"), url.indexOf("3306")).replace(":", "").replace("//", "").replace("mysql", "");
        }else{
            return url.substring(url.indexOf("mysql"), url.indexOf("3307")).replace(":", "").replace("//", "").replace("mysql", "");
        }
    }

    /**
     * 获取数据库名
     */
    private String getDataBaseName() {
        String url = druidDataSource.getUrl();
        if(url.indexOf("3306")>=0){
            return url.substring(url.indexOf("3306"), url.indexOf("?")).replaceAll("/", "").replaceAll("3306", "");
        }else{
            return url.substring(url.indexOf("3307"), url.indexOf("?")).replaceAll("/", "").replaceAll("3307", "");
        }
    }
}
